#!/bin/bash

#xybackup is a software for backup your operation system. Do you like it?
#If you have any question, please tell us at xiyoulinux@googlegroups.com
#welcome to use xybackup

version=1.10

me=$(whoami)
if [[ $me != "root" ]];then
	echo "Please use root!"
	exit 1
fi

configfile()
{
	cd ~/
	if [[ -e .xybackup ]];then
		echo ".xybackup had touch"
	else
		touch .xybackup
	fi 
}	

useage()
{
	echo "Welcome to use xybackup"	
}

xyerror()
{
	echo "error"
}

xyrsync()
{
	echo "Please input your server's name:"
	read server

	user=$(whoami)
	cd || exit 1
	for file in "$@" ;do
        	rsync -aHPvz "${user}@${server}:./${file}" "./${file}"
	done
}

xyb()
{
	if [[ $1 != "a" ]]; then
		echo "Please input a direction will be backup."
		read bdirection
	else
		mkdir /home/backup
	fi
	
}

mytar()
{
	if [[ $1 == "a" ]]; then
		tar -cf all.`date +%Y.%m.%d`.tar /
		if [[ -d /home/xybackup ]]; then
			mv all.*.tar /home/xybackup
		else
			mkdir /home/backup
			mv all.*.tar /home/xybackup
		fi
	else
		xyb
		echo "Please your filename that is going to backup it"
		read backfile
		tar -cf backfile.`date +%Y.%m.%d`.tar $backfile
		mv backfile.*.tar /home/backup
	fi	
}

selet()
{
	echo "If you input char that is b, it will be backup your operation system in your computer"
	echo "If you input char that is r, it will be backup your operation system in others computer"
	echo "Please selet b or r:"
	read bor
	case $bor in
		b) echo "b";;
		r) xyrsync;;
		*) useage;;
	esac
}

autobackup()
{
	xyb
	mytar
}

headbackup()
{
	echo "headbackup"
}

xybackuplog()
{
	cd /var/log/
	if [[ -e xybackup ]];then
		echo "xybackup had touch in /var/log"
		echo "Do you delete it?"
		echo "y|n"
		read yon
		case "$yon" in
			y|Y|yes|YES)	
				rm /var/log/xybackup
				touch xybackup;;
			n|N|no|NO)
				echo "Please touch a log of file for xybackup,thanks"
				read logname
				touch $logname;;
			*) useage;;
		esac
	else
		touch xybackup
	fi
}

case "$1" in
	-a)	autobackup
		xybackuplog;;
	-b)	headbackup;;
	-v)	echo "version is $version";;
	-h|*)	useage;;
esac

if [[ $1 != "-a" && $1 != "-h" && $1 != "-v" ]]; then
	case "$2" in
		-b) 	headbackup;;
		*)	useage;;
	esac
fi
	
